package demo.replace.ifelse;

import com.google.common.collect.ImmutableMap;

import java.util.Map;

/**
 * 充值处理工厂类
 * 策略模式 + 工厂模式 + 单例模式
 * @author dingwen
 * @date 2021/08/15
 */
public class RechargeFactory {
    static Map<String,Recharge> map;

    static {
        map = ImmutableMap.of(
                "1000.0",new OneCharge(),
                "2000.0",new TwoCharge(),
                "5000.0",new FiveCharge(),
                "10000.0",new TenCharge()
        );
    }


    /**
     * 单例持有者
     *
     * @author dingwen
     * @date 2021/08/15
     */
    public static class SingletonHolder{
        public static RechargeFactory rechargeFactory = new RechargeFactory();
    }

    /**
     * 获得实例
     *
     * @return {@link RechargeFactory}
     */
    public static RechargeFactory getInstance(){
        return SingletonHolder.rechargeFactory;
    }

    /**
     * 得到充值处理策略
     *
     * @param money 钱
     * @return {@link Recharge}
     */
    public Recharge getRecharge(Double money){
        return map.get(money.toString());
    }


}
